<template>
  <div class="app-container">
    <el-row>
      <el-col :span="16">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scxx:standard:add']"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scxx:standard:edit']"
        >修改</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scxx:standard:remove']"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scxx:standard:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="isUse">
            <el-select v-model="queryParams.isUse" placeholder="请选择是否启用" clearable>
              <el-option
                v-for="dict in dict.type.is_use"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="mbName">
            <el-input
              v-model="queryParams.mbName"
              placeholder="请输入模板名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="standardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板编码" align="center" prop="mbCode" />
      <el-table-column label="模板名称" align="center" prop="mbName" />
      <el-table-column label="模板类型" align="center" prop="mbTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mb_type" :value="scope.row.mbTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="other" />
      <el-table-column label="是否启用" align="center" prop="isUse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.isUse"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:standard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:standard:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检验标准对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @open="openChange" @close="closeChange">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="模板名称" prop="mbName">
              <el-input v-model="form.mbName" placeholder="请输入模板名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板类型" prop="mbTypecode">
              <el-select v-model="form.mbTypecode" placeholder="请选择模板类型">
                <el-option
                  v-for="dict in dict.type.mb_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select v-model="form.isUse" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in dict.type.is_use"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="other">
              <el-input v-model="form.other" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider></el-divider>
      <el-row style="margin-top: 5px">
        <el-button type="primary" @click="openCpChoose();" size="mini" plain>选择产品</el-button>
      </el-row>
      <el-row style="margin-top: 5px">
        <span style="font-weight: bold">
          适用产品:
        </span>
          <el-tag
            v-for="mess in cpTime"
            :key="mess.cpCode"
            closable
            style="margin: 5px;user-select: none"
            @close="delCpTags(mess.cpCode)"
          >
            {{ mess.cpName }}
          </el-tag>
      </el-row>
      <el-divider></el-divider>
      <el-row style="margin-top: 5px">
        <el-button type="primary" @click="openXmChoose();" size="mini" plain>选择项目</el-button>
      </el-row>
      <el-row style="margin-top: 5px">
        <span style="font-weight: bold">
          检验项目:
        </span>
        <el-tag
          v-for="mess in xmTime"
          :key="mess.jcCode"
          closable
          style="margin: 5px;user-select: none"
          @close="delXmTags(mess.jcCode)"
        >
          {{ mess.xmCode }}
        </el-tag>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    选择产品-->
    <el-dialog
      title="选择产品"
      :visible.sync="dialogVisibleCp"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="getCpList()"
                        v-model="getCpParams.cpName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="getCpList()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="getCpParams.cpName='';getCpList();" type="info">重置</el-button>
            </el-col>
          </el-row>

        </el-header>
        <el-main>

          <!-- 展示产品信息 -->
          <el-table
            ref="multipleTableCp"
            :data="tableDataCp"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="cpTableSelect">
            <el-table-column
              type="selection">
            </el-table-column>
            <el-table-column
              prop="cpName"
              label="产品名称">
            </el-table-column>
            <el-table-column
              prop="cpTypeName"
              label="产品类型">
            </el-table-column>
            <el-table-column
              prop="cpType"
              label="产品型号">
            </el-table-column>
          </el-table>
          <pagination
            v-show="cpTotal>0"
            :total="cpTotal"
            :page.sync="getCpParams.pageNum"
            :limit.sync="getCpParams.pageSize"
            @pagination="getCpList()"
          />
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button @click="toggleSelectionCp()">重置</el-button>
        <el-button @click="cpClose">取 消</el-button>
        <el-button type="primary" @click="cpSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!--    选择项目-->
    <el-dialog
      title="选择项目"
      :visible.sync="dialogVisibleXm"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="getXmList()"
                        v-model="getXmParams.xmCode"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="getXmList()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="getXmParams.xmCode='';getXmList();" type="info">重置</el-button>
            </el-col>
          </el-row>

        </el-header>
        <el-main>

          <!-- 展示产品信息 -->
          <el-table
            ref="multipleTableXm"
            :data="tableDataXm"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="xmTableSelect">
            <el-table-column
              type="selection">
            </el-table-column>
            <el-table-column
              prop="xmCode"
              label="项目名称">
            </el-table-column>
            <el-table-column
              prop="xmNice"
              label="参数标准">
            </el-table-column>
          </el-table>
          <pagination
            v-show="xmTotal>0"
            :total="xmTotal"
            :page.sync="getXmParams.pageNum"
            :limit.sync="getXmParams.pageSize"
            @pagination="getXmList()"
          />
        </el-main>
      </el-container>
      <span slot="footer" class="dialog-footer">
        <el-button @click="toggleSelectionXm()">重置</el-button>
        <el-button @click="xmClose">取 消</el-button>
        <el-button type="primary" @click="xmSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listStandard, getStandard, delStandard, addStandard, updateStandard, getCp } from "@/api/scxx/standard";
import { listChecklist, getChecklist, delChecklist, addChecklist, updateChecklist } from "@/api/scxx/checklist";

export default {
  name: "Standard",
  dicts: ['is_use', 'mb_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验标准表格数据
      standardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mbName: null,
        mbTypecode: null,
        other: null,
        isUse: null,
        isDelete: null
      },
      // 表单参数
      form: {
        cp:[],
        xm:[]
      },
      // 表单校验
      rules: {
        mbName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        mbTypecode: [
          { required: true, message: "模板类型不能为空", trigger: "change" }
        ],
        isUse: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      },
      // 班组产品标签
      cps: [],
      // 临时产品
      cpTime: [],
      // 控制选择产品的弹出层
      dialogVisibleCp: false,
      // 产品数据
      tableDataCp: [],
      // 选中的产品数据
      multipleSelectionCp: [],
      // 查询人员参数
      getCpParams: {
        pageNum: 1,
        pageSize: 10,
        // 弹出层搜索框值
        cpName:""
      },
      // 总条数
      cpTotal: 0,
      // 检查项目标签
      xms: [],
      // 临时项目
      xmTime: [],
      // 控制检查项目的弹出层
      dialogVisibleXm: false,
      // 项目数据
      tableDataXm: [],
      // 选中的项目数据
      multipleSelectionXm: [],
      // 查询项目参数
      getXmParams: {
        pageNum: 1,
        pageSize: 10,
        // 弹出层搜索框值
        xmCode:""
      },
      // 总条数
      xmTotal: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 点击选择产品时触发
    async openCpChoose(){
      this.dialogVisibleCp=true;
      await this.getCpList()
      const selection = []
      this.cpTime.forEach(e => {
        this.tableDataCp.forEach(e1 => {
          if(e.cpCode == e1.cpCode) {
            selection.push(e1)
          }
        })
      })
      selection.forEach(e => {
        this.$refs.multipleTableCp.toggleRowSelection(e, true)
      })
    },
    // 点击选择项目时触发
    async openXmChoose(){
      this.dialogVisibleXm=true;
      await this.getXmList()
      const selection = []
      this.xmTime.forEach(e => {
        this.tableDataXm.forEach(e1 => {
          if(e.jcCode == e1.jcCode) {
            selection.push(e1)
          }
        })
      })
      selection.forEach(e => {
        this.$refs.multipleTableXm.toggleRowSelection(e, true)
      })
    },
    // 新增修改对话框关闭时
    closeChange(){
      this.cpTime = [];
      this.cps = [];
      this.xmTime = [];
      this.xms = []
    },
    // 新增修改对话框打开时
    openChange(){
      this.cpTime = [...this.cps]
      this.xmTime = [...this.xms]
    },
    // 删除成员
    delCpTags(cpCode){
      this.cpTime = this.cpTime.filter((item) => {
        return item.cpCode != cpCode;
      });
    },
    // 删除成员
    delXmTags(jcCode){
      this.xmTime = this.xmTime.filter((item) => {
        return item.jcCode != jcCode;
      });
    },
    // 提交成员数据
    cpSubmit(){
      this.getCpParams.cpName = ""
      this.dialogVisibleCp = false;
      this.cpTime = [...this.multipleSelectionCp]
      this.multipleSelectionCp = []
    },
    // 关闭选择成员窗口
    cpClose(){
      this.multipleSelectionCp = []
      this.getCpParams.cpName = ""
      this.$refs.multipleTableCp.clearSelection()
      this.dialogVisibleCp = false;
    },
    // 提交成员数据
    xmSubmit(){
      this.getXmParams.xmCode = ""
      this.dialogVisibleXm = false;
      this.xmTime = [...this.multipleSelectionXm]
      this.multipleSelectionXm = []

    },
    // 关闭选择成员窗口
    xmClose(){
      this.multipleSelectionXm = []
      this.getXmParams.xmCode = ""
      this.$refs.multipleTableXm.clearSelection()
      this.dialogVisibleXm = false;
    },
    // 产品取消选中
    toggleSelectionCp() {
      this.$refs.multipleTableCp.clearSelection();
    },
    // 产品取消选中
    toggleSelectionXm() {
      this.$refs.multipleTableCp.clearSelection();
    },
    // 获取选中产品的数据
    cpTableSelect(selection){
      this.multipleSelectionCp = selection;
    },
    // 获取选中产品的数据
    xmTableSelect(selection){
      this.multipleSelectionXm = selection;
    },
    // 查询所有成员
    async getCpList(){
      const res = await getCp(this.getCpParams)
      this.tableDataCp = res.rows;
      this.cpTotal = res.total;
    },
    // 查询所有成员
    async getXmList(){
      const res = await listChecklist(this.getXmParams)
      this.tableDataXm = res.rows;
      this.xmTotal = res.total;
    },
    /** 查询检验标准列表 */
    getList() {
      this.loading = true;
      listStandard(this.queryParams).then(response => {
        this.standardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.cpTime = []
      this.cps = []
      this.xmTime = []
      this.xms = []
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mbCode: null,
        mbName: null,
        mbTypecode: null,
        other: null,
        isUse: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mbCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检验标准";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mbCode = row.mbCode || this.ids
      getStandard(mbCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.cps = [...this.form.cp]
        this.xms = [...this.form.xm]
        this.title = "修改检验标准";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.cps = this.cpTime
      this.form.cp = this.cps
      this.xms = this.xmTime
      this.form.xm = this.xms
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mbCode != null) {
            updateStandard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStandard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mbCodes = row.mbCode || this.ids;
      this.$modal.confirm('是否确认删除检验标准编号为"' + mbCodes + '"的数据项？').then(function() {
        return delStandard(mbCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scxx/standard/export', {
        ...this.queryParams
      }, `standard_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scope lang="scss">
.yu {
  .el-form-item__content {
    display: flex;
  }
}
</style>
